<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="author" content="Zhou Wei <zromyk@163.com>">
  <title>C++面向对象</title>
  <link rel="shortcut icon" href="/favicon.ico">
  <link rel="stylesheet" href="/style/pure.css">
  <link rel="stylesheet" href="/style/main.css">
  <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
  <script src='/style/latest.js?config=TeX-MML-AM_CHTML'></script>
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script src='/style/Valine.min.js'></script>
  <link href="https://apps.bdimg.com/libs/highlight.js/9.1.0/styles/default.min.css" rel="stylesheet">
  <script src="https://apps.bdimg.com/libs/highlight.js/9.1.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  <script src="https://cdn.geogebra.org/apps/deployggb.js"></script>
  <script src="https://cdn1.lncld.net/static/js/2.5.0/av-min.js"></script>
  <script src='/style/readTimes.js'></script>
</head>
<body>
  <div id="menu-background"></div>
  <div id="menu">
    <div class="pure-menu pure-menu-horizontal">
  <div id="menu-block">
    <ul class="pure-menu-list">
      <a class="pure-menu-heading" href="/index.html">ZROMYK</a>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/index.html">主页</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/archive/index.html">归档</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/download/index.html">下载</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/feedback/index.html">反馈</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/about/index.html">关于我</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="https://github.com/zromyk"><i class="fa fa-github" style="font-size:32px"></i></a>
</li>

    </ul>
  </div>
</div>

  </div>
  <div id="layout">
    <div class="content">
      <div id="content-articles">
  <h1 id="C++面向对象" class="content-subhead">C++面向对象</h1>
  <p>
    <span>2020-09-20</span>
    <span><span class="post-category post-category-c">C++</span></span>
    <span id="/public/article/C++面向对象.html" class="leancloud_visitors" style="display:none" data-flag-title="C++面向对象"></span>
  </p>
  <h3 id="_1">顺序容器</h3>
<div class="pure-table-scrollable"><table class="pure-table pure-table-horizontal">
<thead>
<tr>
<th>顺序容器</th>
<th>名称</th>
<th>访问规则</th>
<th>访问速度</th>
</tr>
</thead>
<tbody>
<tr>
<td><div style="overflow-x: auto; max-width:100%; display:inline;"><code>vector</code></div></td>
<td>可变大小数组</td>
<td>支持快速随机访问</td>
<td>在尾部之外的位置插入或删除元素可能很慢</td>
</tr>
<tr>
<td><div style="overflow-x: auto; max-width:100%; display:inline;"><code>deque</code></div></td>
<td>双端队列</td>
<td>支持快速随机访问</td>
<td>在头尾位置插入、删除速度很快</td>
</tr>
<tr>
<td><div style="overflow-x: auto; max-width:100%; display:inline;"><code>list</code></div></td>
<td>双向链表</td>
<td>只支持双向顺序访问</td>
<td>在 <div style="overflow-x: auto; max-width:100%; display:inline;"><code>list</code></div> 中任何位置进行插入 / 删除操作速度都很快</td>
</tr>
<tr>
<td><div style="overflow-x: auto; max-width:100%; display:inline;"><code>forward_list</code></div></td>
<td>单向链表</td>
<td>只支持双向顺序访问</td>
<td>在链表的任何位置进行插入 / 删除操作速度都很快</td>
</tr>
<tr>
<td><div style="overflow-x: auto; max-width:100%; display:inline;"><code>array</code></div></td>
<td>固定大小数组</td>
<td>支持快速随机访问</td>
<td>不能添加或删除元素</td>
</tr>
<tr>
<td><div style="overflow-x: auto; max-width:100%; display:inline;"><code>string</code></div></td>
<td>与 <div style="overflow-x: auto; max-width:100%; display:inline;"><code>vector</code></div> 相似容器</td>
<td>专门用于保存字符</td>
<td>在尾部插入 / 删除速度都很快</td>
</tr>
</tbody>
</table></div>
<h2 id="vector"><div style="overflow-x: auto; max-width:100%; display:inline;"><code>vector</code></div></h2>
<h4 id="1">1. 初始化方法</h4>
<pre><code class="pre-wrap"><div style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-c++">vector&lt;int&gt; v1;
vector&lt;int&gt; v2(v1);
vector&lt;int&gt; v2 = v1;
vector&lt;int&gt; v3(n, val);
vector&lt;int&gt; v4(n);
vector&lt;int&gt; v5{a, b, c, ...};
vector&lt;int&gt; v5 = {a, b, c, ...};
</code></div></code></pre>
<h4 id="2">2. 方法</h4>
<pre><code class="pre-wrap"><div style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-c++">v.empty(); // 迭代器 v.begin() == v.end() 等价于 empty()
v.size();
v.push_back();
v[n];
v1 = v2;
v1 = {a, b, c, ...};
v1 == v2;
v1 != v2;
&lt;, &gt;, &lt;=, =&gt;
</code></div></code></pre>
<h4 id="3">3. 迭代器</h4>
<pre><code class="pre-wrap"><div style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-c++">*iter;
iter-&gt;mem;
++iter;
--iter;
iter1 == iter2;
iter1 != iter2;

iter + n;
iter - n;
iter += n;
iter -= n;
iter1 - iter2;
&lt;, &gt;, &lt;=, =&gt;
</code></div></code></pre>
</div>
<div id="nav">
  <div class="navigation">
  <ul class="pure-menu-list">
    <li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.25em;" href="#_1">顺序容器</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 0.75em;" href="#vector"><div style="overflow-x: auto; max-width:100%; display:inline;"><code>vector</code></div></a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.75em;" href="#1">1. 初始化方法</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.75em;" href="#2">2. 方法</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.75em;" href="#3">3. 迭代器</a>
</li>

  </ul>
</div>

</div>
 
    </div>
  </div>
  <div id="footer">
    <div class="legal pure-g">
  <div class="pure-u-1 u-sm-1-2">
    <p class="legal-license"><a href="https://beian.miit.gov.cn/#/Integrated/index">浙ICP备2020038748号</a></p>
  </div>
  <div class="pure-u-1 u-sm-1-2">
    <p class="legal-links"><a href="https://github.com/zromyk">GitHub</a></p>
    <p class="legal-copyright">Copyright © 2020 Wei Zhou. 保留所有权利。</p>
  </div>
</div>
  </div>
</body>
</html>
